;; -------- update ESS (for SAS on Win) --------
(use-package ess-site
  :ensure ess
  :init
  (font-lock-add-keywords 'SAS-mode '(("create\\|from\\|having\\|group by\\|order by\\|when" . 'font-lock-keyword-face)))
  (defun symbol-overlay-ignore-function-sas (symbol)
    "Determine whether SYMBOL should be ignored (SAS Language)."
    (symbol-overlay-match-keyword-list
     symbol
     '("array" "by" "compare" "compress" "contents" "create" "data"
       "delete" "do" "drop" "else" "end" "find" "format" "from" "group"
       "having" "if" "in" "index" "join" "keep" "label" "left" "length"
       "libname" "max" "mean" "means" "merge" "min" "missing" "on"
       "options" "order" "out" "proc" "put" "quit" "random" "rename"
       "retain" "return" "right" "run" "select" "set" "sort" "sql" "sum"
       "summary" "table" "tables" "then" "union" "var" "value" "when"
       "where")))
  (with-eval-after-load 'symbol-overlay
    (add-to-list 'symbol-overlay-ignore-functions '(SAS-mode . symbol-overlay-ignore-function-sas)))
  (defun p1uxtar-SAS-goto-sas ()
    (interactive)
    (find-file
     (concat
      (dired-replace-in-string "Log" "Program"
                               (file-name-sans-extension (buffer-file-name)))
      ".sas")))
  (defun p1uxtar-SAS-goto-log ()
    (interactive)
    (find-file
     (concat
      (dired-replace-in-string "Program" "Log"
                               (file-name-sans-extension (buffer-file-name)))
      ".log")))
  (defun p1uxtar-SAS-newline ()
    (interactive)
    (progn
      (search-forward ";")
      (newline-and-indent)))
  (defun p1uxtar-SAS-semicolon ()
    (interactive)
    (progn
      (sas-indent-line)
      (mwim-end-of-code)
      (insert ";")))
  (defun p1uxtar-copy-kf-2-ch ()
    (interactive)
    (thing-copy-whole-buffer)
    (let* ((cfile (expand-file-name (buffer-file-name)))
           (cpath (nth 1 (reverse (split-string cfile "/"))))
           (dfile (file-name-nondirectory (buffer-file-name))))
      (when (string= "KF" cpath)
        (let* ((dpath "../CH/"))
          (find-file (concat dpath dfile))
          (erase-buffer)
          (yank)
          (goto-char (point-min))
          (while (search-forward "kf" nil t)
            (replace-match "ch"))
          (find-file cfile)))))
  (defun p1uxtar-SAS-vvalue ()
    "Get the formatted value that the variable under cursor."
    (interactive)
    (let (pt)
      (skip-chars-backward "-_A-Za-z0-9")
      (setq pt (point))
      (skip-chars-forward "-_A-Za-z0-9")
      (set-mark pt))
    (thing-copy-region-or-line)
    (insert "_v=vvalue();\n")
    (backward-char 3)
    (ess-yank)
    (forward-line)
    (mwim-beginning-of-code-or-line))
  (add-hook 'SAS-mode-hook
            (lambda ()
              (local-set-key (kbd "C-e") 'mwim-end-of-code-or-line)
              (local-set-key (kbd "C-c v") 'p1uxtar-SAS-vvalue)
              (local-set-key (kbd "C-c C-p") 'beginning-of-sas-proc)
              (local-set-key (kbd "C-c C-n") 'next-sas-proc)
              (local-set-key (kbd "M-<return>") 'p1uxtar-SAS-newline)
              (local-set-key (kbd "C-<return>") 'p1uxtar-SAS-newline)
              (local-set-key (kbd "<f5>") 'p1uxtar-SAS-goto-sas)
              (local-set-key (kbd "<f6>") 'p1uxtar-SAS-goto-log)))
  (require '+cdisc-mode)
  :mode
  ("\\.log" . SAS-log-mode)
  ("\\.lst" . ess-listing-minor-mode)
  :hook
  ((SAS-mode . (lambda ()
                 ;; (electric-operator-mode)
                 (flycheck-mode -1)
                 (linum-mode))))
  :config
  (electric-pair-mode 1)
  (let* ((SASpath "/SASHome/SASFoundation/9.4/sas.exe"))
    (if (string= (system-name) "RZ-ZZ-PC-PG004")
        (setq-default ess-sas-submit-command (concat "d:" SASpath))
      (setq-default ess-sas-submit-command (concat "C:/Progra~1" SASpath))))
  :bind
  (("<f7>" . ess-sas-goto-lst)
   ("<f8>" . ess-sas-submit)))


;; custom switch & highlight functions


;; (defun p1uxtar-highlight-sas-log ()
;;   (interactive)
;;   (highlight-regexp "^ERROR.*:" font-lock-warning-face)
;;   (highlight-regexp "^NOTE:" font-lock-variable-name-face))
;; (add-hook 'SAS-log-mode-hook #'p1uxtar-highlight-sas-log)


;; (defun p1uxtar-get-sas-base-name ()
;;   (when (string-match-p "\\.log\\'\\|\\.lst\\'\\|\\.sas\\'" (buffer-name))
;;     (let ((current-sas-name (file-name-base (buffer-name))))
;;       (concat current-sas-name ".sas"))))
;; (defun p1uxtar-switch-sas-file ()
;;   (interactive)
;;   (if (get-buffer (p1uxtar-get-sas-base-name))
;;       (switch-to-buffer-other-window (p1uxtar-get-sas-base-name))
;;     (find-file-other-window (p1uxtar-get-sas-base-name))))

;; (defun p1uxtar-get-sas-log-name ()
;;   (when (string-match-p "\\.log\\'\\|\\.lst\\'\\|\\.sas\\'" (buffer-name))
;;     (let ((current-sas-name (file-name-base (buffer-name))))
;;       (concat current-sas-name ".log"))))
;; (defun p1uxtar-switch-sas-log ()
;;   (interactive)
;;   (if (get-buffer (p1uxtar-get-sas-log-name))
;;       (switch-to-buffer-other-window (p1uxtar-get-sas-log-name))
;;     (find-file-other-window (p1uxtar-get-sas-log-name))))

;; (defun p1uxtar-get-sas-lst-name ()
;;   (when (string-match-p "\\.log\\'\\|\\.lst\\'\\|\\.sas\\'" (buffer-name))
;;     (let ((current-sas-name (file-name-base (buffer-name))))
;;       (concat current-sas-name ".lst"))))
;; (defun p1uxtar-switch-sas-lst ()
;;   (interactive)
;;   (if (get-buffer (p1uxtar-get-sas-lst-name))
;;       (switch-to-buffer-other-window (p1uxtar-get-sas-lst-name))
;;     (find-file-other-window (p1uxtar-get-sas-lst-name))))

(provide '+ESS-SAS)
